import dayjs from 'dayjs';
import mongoose, { Schema } from 'mongoose';

export interface IArticle {
	_id: string;
	cover: string;
	title: string;
	content: string;
	categoryId: mongoose.Types.ObjectId;
	isTop: boolean;
	createdAt: string;
	updatedAt: string;
}

const ArticleSchema = new Schema<IArticle>(
	{
		cover: { type: String, required: true },
		title: { type: String, required: true, unique: true },
		content: { type: String, required: true },
		categoryId: { type: Schema.Types.ObjectId, ref: 'Category', required: true },
		isTop: { type: Boolean, default: false },
		createdAt: {
			type: String,
			default: () => dayjs().format('YYYY年MM月DD日 HH:mm:ss'),
		},
		updatedAt: {
			type: String,
			default: () => dayjs().format('YYYY年MM月DD日 HH:mm:ss'),
		},
	},
	{
		collection: 'article',
	}
);

export const ArticleModel = mongoose.model<IArticle>('Article', ArticleSchema);
